package com.summer.hundred;

import java.math.BigInteger;

public class que8 {
    public static void main(String[] args) {
            String s ="20000000000000000000";

        System.out.println(myAtoi(s));

    }
    public static int myAtoi(String s) {
        long MAX =2147483647;
        long MIN = -2147483648;
        s = s.trim();
        String ss = "";
        if (s.length()<2){
            if (s.length()==0){
                return 0;
            }
            else if (s.charAt(0)>'0'&&s.charAt(0)<'9'){
                ss = s.charAt(0)+"";
                int snum = Integer.parseInt(ss);
                return snum;
            }else {
                return 0;
            }
        }else {
            if (s.charAt(0)=='-'||s.charAt(0)=='+'){
                if (s.charAt(1)<'0'||s.charAt(1)>'9'){
                    return 0;
                }
            }
            if (s.charAt(0)!='-'&&s.charAt(0)!='+'&&(s.charAt(0)<'0'||s.charAt(0)>'9')) return 0;
                Character fuHao =' ';
                for(int i = 0;i < s.length();i++){
                    if(s.charAt(i)=='-'||s.charAt(i)=='+') {
                        fuHao = s.charAt(i);
                        break;
                    }
                }
                if (fuHao == null) fuHao='+';
                for(int i = 0;i < s.length();i++){
                    if(s.charAt(i)>='0'&&s.charAt(i)<='9') {
                        ss+=s.charAt(i)+"";
                        if(((i+1)>=s.length()||s.charAt(i+1)<'0'||s.charAt(i+1)>'9')){
                            break;
                        }
                    }
                }
                System.out.println(ss);
            if (fuHao=='-'){
                BigInteger ssB=new BigInteger(ss);
                BigInteger ssMin= BigInteger.valueOf(-MIN);
                int c = ssB.compareTo(ssMin);
                if (c==-1) return -ssB.intValue();
                if (c==0) return -ssB.intValue();
                if (c==1) return -ssMin.intValue();
            }else {
                BigInteger ssB=new BigInteger(ss);
                BigInteger ssMax= BigInteger.valueOf(MAX);
                int c = ssB.compareTo(ssMax);
                if (c==-1) return ssB.intValue();
                if (c==0) return ssB.intValue();
                if (c==1) return ssMax.intValue();
            }
        }
        return 0;
    }
}
